package com.chenyun.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.constants.SysConstants;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import com.chenyun.system.model.SysTenantType;
import com.chenyun.system.param.TenantParam;
import com.chenyun.system.param.TenantQueryPage;
import com.chenyun.system.param.TenantTypeParam;
import com.chenyun.system.service.ISysTenantService;
import com.chenyun.system.service.ISysTenantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * SysTenantController：
 * Description:
 *
 * @author: Lonni
 * @date: 2023/12/18 0018 11:19
 */
@RestController
@RequestMapping("tenant")
@Api(tags = "租户管理-admin")
public class SysTenantController implements Serializable {

    @Autowired
    private ISysTenantService tenantService;

    @Autowired
    private ISysTenantTypeService tenantTypeService;


    @PostMapping("type/add")
    @ApiOperation("租户套餐类型-添加")
    @Log(title = "添加租户类型", businessType = BusinessTypeEnum.INSERT, desc = "'添加租户类型'")
    @SaCheckPermission("system:tenant:type:add")
    public AjaxResult<Integer> addTenantType(@RequestBody @Valid TenantTypeParam tenantType) {
        Integer integer = tenantTypeService.crateTenantType(tenantType);
        return AjaxResult.SUCCESS(integer);
    }

    @PostMapping("type/page")
    @ApiOperation("租户套餐类型-分页查询")
    @SaCheckPermission("system:tenant:type:page")
    public PageResult pageType(@RequestBody TenantTypeParam tenantTypeParam) {
        return tenantTypeService.pageList(tenantTypeParam);
    }

    @PostMapping("type/list")
    @ApiOperation("租户套餐类型")
    @SaCheckPermission("system:tenant:type:list")
    public AjaxResult<List<SysTenantType>> listType(@RequestBody TenantTypeParam tenantTypeParam) {
        return AjaxResult.SUCCESS(tenantTypeService.list(tenantTypeParam));
    }


    @PostMapping("type/edit")
    @ApiOperation("租户套餐类型-修改")
    @Log(title = "修改租户类型", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:tenant:type:edit")
    public AjaxResult editTenantType(@RequestBody @Valid TenantTypeParam tenantType) {
        tenantTypeService.updateTenantType(tenantType);
        return AjaxResult.SUCCESS();
    }

    @PostMapping("type/auth/{typeId}")
    @ApiOperation("修改类型租户授权")
    @Log(title = "修改租户授权", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:tenant:type:auth")
    public AjaxResult authTenantType(@PathVariable("typeId") Integer typeId,
                                    @RequestBody List<String> menuIds) {
        tenantTypeService.authTenantType(typeId,menuIds);
        return AjaxResult.SUCCESS();
    }



    @ApiOperation("添加租户")
    @PostMapping("create")
    @Log(title = "添加租户", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:tenant:add")
    public AjaxResult addTenant(@RequestBody TenantParam param) {
        Boolean tenant = tenantService.createTenant(param);
        return AjaxResult.SUCCESS(tenant);
    }

    @ApiOperation("修改租户")
    @PostMapping("edit")
    @Log(title = "修改租户", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:tenant:edit")
    public AjaxResult editTenant(@RequestBody TenantParam param) {
        Boolean tenant = tenantService.updateTenant(param);
        return AjaxResult.SUCCESS(tenant);
    }

    @ApiOperation("删除租户")
    @PostMapping("del")
    @Log(title = "删除租户", businessType = BusinessTypeEnum.DELETE)
    @SaCheckPermission("system:tenant:del")
    public AjaxResult delTenant(@RequestBody List<Integer> ids) {
        Boolean tenant = tenantService.delTenant(ids);
        return AjaxResult.SUCCESS(tenant);
    }

    @ApiOperation("查询租户-分页")
    @PostMapping("page")
    @SaCheckPermission("system:tenant:page")
    public PageResult pageResult(@RequestBody TenantQueryPage param) {
        return tenantService.pageList(param);
    }


}



